import e from "./view_container";
import t from "./mat3";

export default class SpinView extends e {
  clone() {
    let e = this,
      t = new SpinView();
    return e.copyProperties(t), t;
  }
  copyProperties(t) {
    let n = this;
    e.prototype.copyProperties.call(n, t),
      (t.originalMat = n.originalMat.clone()),
      (t.clockwise = n.clockwise),
      t.aps(n.aps()),
      t.angle(n.angle()),
      n.playing ? t.play() : t.pause();
  }
  dealloc() {
    let t = this;
    e.prototype.dealloc.call(t), (t.originalMat = null);
  }
  originalMat = null;
  updatePropsFromXML(t) {
    let n = this;
    e.prototype.updatePropsFromXML.call(n, t),
      (n.originalMat = n.matrix().clone()),
      t.is("[clockwise]")
        ? (n.clockwise = t.attr("clockwise") == "true")
        : (n.clockwise = !0);
    if (t.is("[aps]")) {
      let r = parseFloat(t.attr("aps"));
      n.aps(r);
    }
    t.is("[auto-play]")
      ? (n.playing = t.attr("auto-play") == "true")
      : (n.playing = !0);
  }
  playing = !1;
  clockwise = !0;
  play() {
    let e = this;
    e.playing = !0;
  }
  pause() {
    let e = this;
    e.playing = !1;
  }
  angleChanged = !1;
  _angle = 0;
  angle(e?) {
    let t = this;
    return (
      typeof e == "number" &&
        ((e = e >= 0 ? e % 360 : 360 - (Math.abs(e) % 360)),
        t._angle != e && ((t._angle = e), (t.angleChanged = !0))),
      t._angle
    );
  }
  _aps = 0;
  aps(e?) {
    let t = this;
    return (
      isNaN(e) == 0 && e != t._aps && ((t._aps = e), t.needToRender(!0)), t._aps
    );
  }
  update(n, r) {
    let i = this;
    e.prototype.update.call(i, n, r);
    if (i.playing) {
      let s = r * i.aps();
      i.angle(i.angle() + (i.clockwise ? 1 : -1) * s);
    }
    if (i.angleChanged) {
      i.angleChanged = !1;
      let o = i.angle() * (Math.PI / 180),
        u = new t();
      (u.a = Math.cos(o)),
        (u.b = Math.sin(o)),
        (u.c = -1 * u.b),
        (u.d = u.a),
        (u.tx = i.width() / 2 - (u.a * i.width()) / 2 - (u.c * i.height()) / 2),
        (u.ty =
          i.height() / 2 - (u.b * i.width()) / 2 - (u.d * i.height()) / 2),
        (u = u.concatWith(i.originalMat)),
        i.matrix(u);
    }
  }
}
